const util = require('../../utils/util.js')
const api = require('../../config/api.js')
const user = require('../../utils/user.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabnum: 1,
    noCurrentGoods: false,  
    nextStartTime: "",  
    currentEndTime: "",
    goodsGroup: [], // 当前商品渲染数组
    remindValue: ['', '', '送'],
    plusFlag: ['', '+', '  ']
  },

  // 选择团购
  navChange: function(e) {
    const {id} = e.currentTarget.dataset
    if(id == 1){
      this.getGroup()
    }else {
      this.getNextGroup()
    }
    this.setData({
      tabnum: id
    })
  },

  // 前往抢购
  goBuy: function(e) {
    const {id, groupid} = e.currentTarget.dataset
    wx.navigateTo({
      url: '../bulkGood/bulkGood?id=' + id + '&groupId=' + groupid
    })
  },

  // 获取当期团购商品
  getGroup: function() {
   return util.request(api.GetGroup).then(res => {
     if (res.errno == 0) {
       if (res.data.hasOwnProperty("goods")) {  // 返回返回商品列表
        const { currentEndTime, nextStartTime, goods } = res.data
        const noCurrentGoods = currentEndTime == "暂无" ? true : false
        let goodsGroup = this.addPercent(goods)
        this.setData({
          currentEndTime,
          nextStartTime,
          noCurrentGoods,
          goodsGroup
        })
       }else { // 未返回商品数组 
         this.setData({
           goodsGroup: "",
           noCurrentGoods: true
         })
       }
     } else {
       wx.showToast({
         title: '网络错误，请稍后再试',
         icon: "none",
         duration: 2000
       })
     } 
   })
  },

  // 获取下一期团购商品
  getNextGroup: function() {
    util.request(api.GetNextGroup).then(res => {
     if(res.errno == 0) {
      if(res.data.hasOwnProperty("goods")){  // 返回返回商品列表
        const {goods} = res.data
        console.log(goods)
        const noCurrentGoods = this.data.nextStartTime == "暂无" ? true : false
        for (let i = 0; i < goods.length; i++) {
          goods[i].hasNumber = 0
          goods[i].percent = 0
        }
        this.setData({
          goodsGroup: goods,
          noCurrentGoods,
        })
      }else {  // 未返回商品列表
        this.setData({
          goodsGroup: "",
          noCurrentGoods: true,
        })
      }
     }else { // errno != 0 的情况下
       this.setData({
         goodsGroup: "",
       })
       wx.showToast({
         title: '网络错误，请稍后再试',
         icon: "none",
         duration: 2000
       })
     }
    })
  },


  // 添加抢购百分比和已抢人数，剩余人数
  addPercent: function (goods) {
    for (let i = 0; i < goods.length; i++) {
      const { orderNumber, leastNumber } = goods[i]
      let hasNumber = leastNumber - orderNumber  // 已抢购的人数
      let percent = hasNumber <= 0 ? "0" : ((Math.round(hasNumber / leastNumber * 10000) / 100.00).toFixed(1))
      goods[i].hasNumber = hasNumber
      goods[i].percent = percent
    }
    return goods
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getGroup()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.getGroup()
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})